package com.wushi.lenovo.asia5b.app_ui.other.dialog;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import com.mvp.base.BaseDialog;
import com.mvp.common.LocalManageUtil;
import com.wushi.lenovo.asia5b.R;
import com.wushi.lenovo.asia5b.app_event.NonetEvent;
import com.wushi.lenovo.asia5b.app_utils.ConnectUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import timber.log.Timber;

/**
 * Created by zhangyuncai on 2020/5/23.
 */
public class NoNetDialog extends BaseDialog {


    public NoNetDialog(Context context) {
        super(context);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_nonet;
    }

    @Override
    protected void initView() {
        EventBus.getDefault().register(this);
        ImageView iv_nonet_bg = findViewById(R.id.iv_nonet_bg);
        if (TextUtils.equals("en", LocalManageUtil.getAppLanguage())) {//如果是英文环境
            iv_nonet_bg.setImageResource(R.drawable.img_nonet_en);
        } else {
            iv_nonet_bg.setImageResource(R.drawable.img_nonet_cn);
        }
        setCancelable(false);
        setCanceledOnTouchOutside(false);

        Window window = getWindow();
        if (window != null) {
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        }
        iv_nonet_bg.setOnClickListener(v -> {
            connect();
        });

    }

    private void connect() {

        if (ConnectUtils.INSTANCE.isConnect(getContext()) ||
                ConnectUtils.INSTANCE.isWifiEnable(getContext()) ||
                ConnectUtils.INSTANCE.isMobileEnable(getContext())
        ) {
            EventBus.getDefault().post(new NonetEvent());
            dismiss();
        } else {
            Timber.d("--->>>>>>>>---当前网络中断中...");
        }
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected int getGravity() {
        return Gravity.CENTER;
    }

    @Override
    protected int getAnimations() {
        return R.style.NoNetAnimation;
    }

    @Override
    protected int getWidthStyle() {
        return WindowManager.LayoutParams.MATCH_PARENT;
    }

    @Override
    protected int getHeightStyle() {
        return WindowManager.LayoutParams.MATCH_PARENT;
    }

    @Subscribe(threadMode = ThreadMode.MAIN) //在ui线程执行
    public void onNonetEvent(NonetEvent event) {
        dismiss();
    }

    @Override
    public void dismiss() {
        EventBus.getDefault().unregister(this);
        super.dismiss();
    }
}
